package org.bookie.framework.components;

import java.util.List;

import org.apache.tapestry.components.Block;
import org.bookie.core.services.impl.Listener;
import org.bookie.core.services.impl.Listener.Event;
import org.bookie.framework.CommonBaseComponent;

/**
 * Tabbed pane component
 *  
 * @author zubairov
 */
public abstract class TabPanel extends CommonBaseComponent
{
	// Persistent
	public abstract String getSelectedBlockId();

	public abstract void setSelectedBlockId( String id );

	public abstract List getBlockIds();
	
	/**
	 * Returns a registered listener
	 * @return
	 */
	public abstract ITabPanelEventListener getListener();

	// Current block id within the loop
	public abstract String getBlockId();

	public Block getSelectedBlock()
	{
		String selectedId = getSelectedTabName();
		return (Block)getContainer().getComponent(selectedId);
	}

	public boolean isTabSelected()
	{
		String selectedId = getSelectedTabName();
		return getBlockId().equals(selectedId);
	}

	/**
	 * @return
	 */
	private String getSelectedTabName()
	{
		String selectedId = getSelectedBlockId();

		if( selectedId == null )
			selectedId = (String)getBlockIds().get(0);
		return selectedId;
	}

	public void doClick( String selectedId )
	{
		setSelectedBlockId(selectedId);
		if (getListener() != null) {
			getListener().tabSelected(selectedId);
		}
		getResponseBuilder().updateComponent(getId());
	}

	
	/**
	 * Called when betslip placed
	 */
	@Listener(Event.BET_SLIP_PLACED)
	public void placedBet() {
		setSelectedBlockId(null);
		getResponseBuilder().updateComponent(getId());
	}
}